home *** CD-ROM | disk | FTP | other *** search
/ CD Concept 6 / CD Concept 06.iso / mac / UTILITAIRE / RLaB / misc / gnuplot.r < prev    next >
Encoding:
Text File  |  1994-10-22  |  39.8 KB  |  1,742 lines  |  [TEXT/????]

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    plot ( A, KeyTitles, I )
  4. //        splot ( X, Y, Z, KeyTitles, I )
  5. //        cont ( X, Y, Z, KeyTitles, I )
  6. //        pstring ( "string", I )
  7. //        postplot ( "file_name", "mode", I )
  8. //        latexplot ( "file_name", "mode", I )
  9. //        printplot ( "printer-name", I )
  10. //        psave ( "file_name", A )
  11. //        pssave ( "file_name", X, Y, Z )
  12. //        multiplot ( rows, columns, I )    !!!!!! Special GNUPLOT-contrib needed !!!!!!
  13. //        nomultiplot ( I )        !!!!!! Special GNUPLOT-contrib needed !!!!!!
  14. //        plotyy ( I )            !!!!!! Special GNUPLOT-contrib needed !!!!!!
  15. //        noplotyy ( I )            !!!!!! Special GNUPLOT-contrib needed !!!!!!
  16. //        pclose ( I )
  17. //        setterm ( "term", I )
  18. //        showplot ( I )
  19. //        showpwin ( )
  20. //        replot ( I )
  21. //        phelp ( "string", I)
  22. //        autoscale ( "axis", I )
  23. //        xrange ( start, end, I )
  24. //        yrange ( start, end, I )
  25. //        zrange ( start, end, I )
  26. //        xtics ( start, end, incr, I ) or xtics (vector, I ) or xtics ( I )
  27. //        noxtics ( I )
  28. //        ytics ( start, end, incr, I ) or ytics (vector, I ) or ytics ( I )
  29. //        noytics ( I )
  30. //        ztics ( start, end, incr, I ) or ytics (vector, I ) or ytics ( I )
  31. //        noztics ( I )
  32. //        loglog ( I )
  33. //        semilogx ( I )
  34. //        semilogy ( I )
  35. //        nolog ( I )
  36. //        xlabel ( "label_name", I )
  37. //        ylabel ( "label_name", I )
  38. //        zlabel ( "label_name", I )
  39. //        title ( "title_name", I )
  40. //        notitle ( "title_name", I )
  41. //        linestyle ( "line_style", I )
  42. //        pformat ( "format-string", "axis", I )
  43. //        key ( x, y, z, I )    (*)
  44. //        nokey ( I )
  45. //        grid ( I )
  46. //        nogrid ( I )
  47. //        axis ( I )
  48. //        noaxis ( I )
  49. //        label ( "label_text", x, y, z, labelnr, I )    (*)
  50. //        nolabel ( labelnr, I )
  51. //        time ( I )
  52. //        notime ( I )
  53.  
  54. //        (*) Be alert how you send these commands. You will get into trouble
  55. //            if you try to send "I" without fully specified coordinates.
  56. //            Do not leave out "z" or "labelnr" for example.
  57.  
  58. // Description:
  59.  
  60. //  The plot function plots numeric data via the GNUPLOT program. The
  61. //  argument A can be a matrix, or a list of matrices, or a string.
  62. //  When A is a matrix, columns 2 through N are plotted versus the 1st
  63. //  column. When A is a list of matrices, each matrix is plotted
  64. //  versus it's own 1st column. When A is a string, the string is sent
  65. //  to GNUPLOT as a command.
  66.  
  67. //    plot ( M )            // plots the contents of matrix M
  68. //    plot ( << M1 ; M2>> )        // plots M1, and M2 on the same graph
  69. //    pstring ( "set noborder" )    // sends the string directly to
  70. //                    // GNUPLOT as a command
  71.  
  72. //  Using a list of matrices as an argument to plot is useful when the
  73. //  the independent variable (1st column) of each matrix is different.
  74.  
  75. //  The second and third, and optional argument to plot denotes the plot process
  76. //  number and sends key-titles. The plot() function will open distinct plot
  77. //  processes when given distinct values of N. When this feature is used in a
  78. //  X-windows environment, the effect is to create separate plot windows.
  79. //  The KeyTitles is a string-vector containing the titles to be plotted as keys.
  80. //  They are chosen beginning at KeyTitles[1] and so on.
  81. //  For example:
  82.  
  83. //     plot ( M );            // plot the contents if matrix M
  84. //     plot ( 2.*M, 1 );        // plot 2 times M, to another window
  85. //    plot (<<M1;M2>>,2,KeyTitles)    // plot M1, and M2 on the same graph in another
  86. //                    // window with the keytitles from KeyTitles
  87.  
  88. //  For 3D-plotting there are several commands. splot() makes a 3D-surface plot
  89. //  and cont() makes a 2D-contour plot. This is in GNUPLOT a 3D-plot with no 
  90. //  surface.
  91. //  
  92. //  The splot() function splots numeric data via the GNUPLOT program. The
  93. //  argument Z is a matrix as functions of vectors X and Y.
  94.  
  95. //    splot ( X, Y, Z )        // splots the contents of matrix Z
  96. //                    // versus X and Y
  97. //    splot ( Z )            // splots the contents of matrix Z
  98. //                    // versus rows and columns
  99. //    splot ( X, Y, [Z1,Z2] )        // splots Z1, and Z2 on the same graph
  100. //    cont ( X, Y, Z )        // splots the contents of matrix Z
  101. //                    // as a 2D-contour-plot
  102. //    cont ( X, Y, [Z1,Z2] )        // splots Z1, and Z2 on the same graph
  103. //                    // as a 2D-contour-plot
  104.  
  105. //  The psave() and pssave() saves the usual temporary file to a specific, 
  106. //  non-temporary file named "file_name". psave() is for 2D-plots and pssave() 
  107. //  is for 3D-plots. This is useful if you wish to save session-output in a 
  108. //  GNUPLOT-way.
  109.  
  110. //  multiplot() allows you to make multiple plots in one window. After giving 
  111. //  the command you enter multimode. After making rows*columns plot commands 
  112. //  you automaticly leaves this mode. plotyy() allows you to make one plot with 
  113. //  two y-axes. You will enter multimode, and after making two plot commands you 
  114. //  leave. The first plot will be on the left axis and the second on the right. 
  115. //  If you want to leave multiplot() or plotyy() before ending the plot commands
  116. //  just type nomultiplot() or noplotyy().
  117. //  If you want a PostScript output from plots made in multimode you do not 
  118. //  use postplot(). This will not work. plotyy() is for 2D-plots only. 
  119. //  GNUPLOT-3.5 DO NOT SUPPORT THESE FEATURES. If you want to use them 
  120. //  you must add GNUPLOT-contributions and recompile GNUPLOT.
  121.  
  122. //  The pclose function closes (destroys)  the I-th GNUPLOT
  123. //  subprocess. If I = "all", all GNUPLOT subprocess are closed.
  124.  
  125. //  The postplot() function generates a PostScript file named "file_name" 
  126. //  of the current I-th plot. "mode" is further postscript definitions to 
  127. //  GNUPLOT if needed, for example "eps color 'Times-Roman' 14" for making
  128. //  a smaller plot in color with Times-Roman-font size 14.
  129. //  The PostScript plot is saved in a file for printing, or whatever. 
  130. //  Immediately after calling postplot(), the GNUPLOT variables
  131. //  term, and output are reset to there former values.
  132. //    printplot() generates a PostScript file and sends it to the printer.
  133. //    latexplot() generates a LaTeX file named "file_name" of the current 
  134. //    I-th plot. 
  135.  
  136. //  The setterm function allows the user to set/reset the I-th plot's
  137. //  term type. The argument "term" is a string that corresponds to one
  138. //  of GNUPLOT's available term types, for example "vttek".
  139.  
  140. //  The showplot function prints the contents of the I-th plot list to
  141. //  stdout.
  142. //    The showpwin function prints the currently existing plotwindows 
  143. //    to stdout.
  144.  
  145. //  A bunch of often uset commands to GNUPLOT has special functions.
  146. //  These could just as well be sent as strings, but this makes life easier.
  147. //  Thes are replot(), xrange(), yrange(), zrange(), xtics(), ytics(), 
  148. //    noxtics(), noytics(), loglog(), semilogx(), semilogy(), nolog(), 
  149. //    autoscale(), xlabel(), ylabel(), zlabel(), 
  150. //    title(), notitle(), linestyle(), pformat(), key(), nokey(), 
  151. //  grid(), nogrid(), axis(), noaxis(), label(), nolabel(), 
  152. //  time() and notime().
  153.  
  154. //    The xtics(), ytics() and ztics() functions sets the tics on the axes. 
  155. //    If it is used as xtics(start,end,incr,I), the tics will be set 
  156. //    for plotwindow I, starting from "start", ending at "end", 
  157. //    with an increment of "incr". 
  158. //    If, however, it is used as xtics([a,b,c,d],I), the tics will 
  159. //    set at "a", "b", "c" and "d" in plotwindow I. 
  160. //    The command xtics(I) will reset the tics in plotwindow I.
  161. //  The noxtics() function results in a tics-less x-axis.
  162.  
  163. //  The plot function is an interface to the GNUPLOT program. The plot
  164. //  function uses temporary files and Rlab's piping capability to
  165. //  transparently plot numerical data via GNUPLOT. Since the plot
  166. //  function is an rfile, it could easily be modified to to use
  167. //  another plotting program.
  168.  
  169. //  To be able to use all commands in this r-file you must add some
  170. //  contributions to GNUPLOT and recompile it. These are "multiplot"
  171. //  and "borders". See tha GNUPLOT FAQ for help.
  172.  
  173. //-------------------------------------------------------------------//
  174.  
  175.  
  176. //
  177. // List to contain plot-object lists.
  178. //
  179.  
  180. static (p);
  181. p = <<>>;
  182.  
  183. //
  184. // Internal plot-related functions
  185. //
  186.  
  187. static (plotl, plotm, splotm, plots);
  188. static (pobj_Create, pobj_Reset, pobj_Destroy);
  189. static (pobj_TmpFileName, pobj_WriteData, pobj_PlotCmd, spobj_PlotCmd);
  190. static (spobj_PlotKeyTitle, pobj_Plot, pobj_SetRm, pobj_Rm);
  191.  
  192.  
  193.  
  194. //
  195. // User interface to plot functionality
  196. //
  197.  
  198. plot = function ( data, titles, N )
  199. {
  200.     local(WaitAMoment)
  201.     if (exist (titles)) {
  202.         if (class (titles) == "num") { N = titles; }
  203.     }
  204.     if (!exist (N)) { N = 0; }    // Always the default plot-object
  205.  
  206. // Check the existence of p.[N]
  207.     if (max (size (N)) != 1) { error ("plot(): N must be a 1-by-1"); }
  208.     if (exist (p.[N])) {
  209.         pobj_Reset (N);
  210.     else
  211.         pobj_Create (N);
  212.     }
  213.  
  214. // Check for multiplot and/or plotyy mode
  215.     if (exist (MultiPlotParameter) || exist (PlotYyParameter)) {
  216. // Delay (RLaB is a bit to fast)
  217.         for(i in 1:1000) {
  218.             WaitAMoment=1;
  219.             clear(WaitAMoment);
  220.         }
  221. // Check for plotyy mode
  222.         if (exist (PlotYyParameter)) {
  223.             if (PlotYyParameter==1) {
  224.                 printf (" Plot on left y-axis done!\n");
  225.                 if (!exist (MultiPlotParameter)) {
  226.                     fprintf (p.[N].prog, "set multiplot\n");
  227.                 }
  228.                 fprintf (p.[N].prog, "set border 7\n");
  229.             else if (PlotYyParameter==2) {
  230.                 printf (" Plot on right y-axis done!\n");
  231.                 fprintf (p.[N].prog, "set border 8\n");
  232.             }}
  233.             PlotYyParameter=PlotYyParameter+1;
  234.         }
  235. // Check for multiplot mode
  236.         if (exist (MultiPlotParameter)) {
  237.             printf (" Plot number %i/%i done!\n",MultiPlotParameter[3],MultiPlotParameter[1]*MultiPlotParameter[2]);
  238.             if (MultiPlotParameter[3]==1) {
  239.                 fprintf (p.[N].prog, "set multiplot\n");
  240.                 fprintf (p.[N].prog, "set size %s,%s\n",1./MultiPlotParameter[2],1./MultiPlotParameter[1]);
  241.             }
  242.             N=MultiPlotParameter[4];
  243.             fprintf (p.[N].prog, "set orgin %s,%s\n",MultiOrginParameter[MultiPlotParameter[3];1],MultiOrginParameter[MultiPlotParameter[3];2]);
  244.             if (exist (PlotYyParameter)) {
  245.                 if (PlotYyParameter==3) {MultiPlotParameter[3]=MultiPlotParameter[3]+1; }
  246.             else
  247.                 MultiPlotParameter[3]=MultiPlotParameter[3]+1;
  248.             }
  249.         }
  250.     }
  251.  
  252.     if (class (data) == "num") {
  253.         fprintf (p.[N].prog, "set noparametric\n");    
  254.         pobj_Rm (N);
  255.         plotm (data, N, 1, titles);
  256.         pobj_Plot (N);
  257.         pobj_SetRm (N);
  258.     else if (class (data) == "string") {
  259.         plots (data, N);
  260.         pobj_Plot (N);
  261.     else if (class (data) == "list") {
  262.         pobj_Rm (N);
  263.         plotl (data, N, titles);
  264.         pobj_Plot (N);
  265.         pobj_SetRm (N);    
  266.     else
  267.         error ("Invalid data for plot()");
  268.     }}}
  269.     
  270. // Check for multiplot and/or plotyy mode
  271. // Check for plotyy mode
  272.     if (exist (PlotYyParameter)) {
  273.         if (PlotYyParameter==3) {
  274.             if (!exist (MultiPlotParameter)) { fprintf (p.[N].prog, "set nomultiplot\n"); }
  275.             fprintf (p.[N].prog, "set border\n");
  276.             clear(PlotYyParameter);
  277.         }
  278.     }
  279. // Check for multiplot mode
  280.     if (exist (MultiPlotParameter)) {
  281.         if (MultiPlotParameter[3]==MultiPlotParameter[1]*MultiPlotParameter[2]+1) {
  282.             fprintf (p.[N].prog, "set nomultiplot\n");
  283.             fprintf (p.[N].prog, "set size %s,%s\n",1,1);
  284.             fprintf (p.[N].prog, "set orgin %s,%s\n",0,0);
  285.             fprintf (p.[N].prog, "set border\n");
  286.             clear(MultiPlotParameter);
  287.             clear(MultiOrginParameter);
  288.         }
  289.     }
  290.  
  291. };
  292.  
  293.  
  294.  
  295. //
  296. // User interface to splot functionality
  297. //
  298.  
  299. splot = function ( datax, datay, datazz, titles, N )
  300. {
  301.     local (flag1, flag2, flag3, flag4, flagx, flagy, WaitAMoment)
  302.  
  303. // Reset flags
  304.     flag1=0;
  305.     flag2=0;
  306.     flag3=0;
  307.     flag4=0;
  308.     flagx=0;
  309.     flagy=0;
  310.  
  311. // Check what has been sent here
  312. // One input!
  313.     if (!exist (datay)) {
  314.         datazz=datax;
  315.         datax=1:datazz.nc;
  316.         datax=datax';
  317.         datay=1:datazz.nr;
  318.         datay=datay';
  319.         flag1=flag1+1;
  320. // Two inputs!
  321.     else if (!exist (datazz)) {
  322.         if (exist (datay)) {
  323.             if (class (datay) == "num") {
  324.                 N = datay;
  325.                 flag2=flag2+0;
  326.             else
  327.                 titles = datay;
  328.                 flag2=flag2+1;
  329.             }
  330.         }
  331.         datazz=datax;
  332.         datax=1:datazz.nc;
  333.         datax=datax';
  334.         datay=1:datazz.nr;
  335.         datay=datay';
  336.         flag2=flag2+1;
  337. // Three inputs!
  338.     else if (!exist (titles)) {
  339.         if (class (datay) != "num") {
  340.             titles = datay;
  341.             N = datazz;
  342.             datazz=datax;
  343.             datax=1:datazz.nc;
  344.             datax=datax';
  345.             datay=1:datazz.nr;
  346.             datay=datay';
  347.             flag3=flag3+1;
  348.         }
  349. // Four inputs!
  350.     else if (!exist (N)) {
  351.         if (class (titles) == "num") {
  352.             N=titles;
  353.             flag4=flag4+1;
  354.         }
  355.     }}}}
  356.     
  357. // Transpose x- and y-vectors if nessecary
  358.     if (datax.nc != 1 ) {
  359.         if (datax.nr != 1 ) {
  360.             error ("Invalid data for x-axis (not a vector)");
  361.         else
  362.             datax = datax';
  363.             flagx=flagx+1;
  364.         }
  365.     }
  366.     if (datay.nc != 1 ) {
  367.         if (datay.nr != 1 ) {
  368.             error ("Invalid data for y-axis (not a vector)");
  369.         else
  370.             datay = datay';
  371.             flagy=flagy+1;
  372.         }
  373.     }
  374.     
  375. // Check how many splots to do
  376.     plotantal=(datazz.nc/datazz.nr)/(datax.nr/datay.nr);
  377.     if (plotantal == int(plotantal)) {
  378.     
  379.         if (!exist (N)) { N = 0; }    // Always the default splot-object
  380.  
  381. // Check the existence of p.[N]
  382.         if (max (size (N)) != 1) { error ("splot(): N must be a 1-by-1"); }
  383.         if (exist (p.[N])) {
  384.             pobj_Reset (N);
  385.         else
  386.             pobj_Create (N);
  387.         }
  388.  
  389. // Check for multiplot and/or plotyy mode
  390.         if (exist (MultiPlotParameter) || exist (PlotYyParameter)) {
  391. // Delay (RLaB is a bit to fast)
  392.             for(i in 1:1000) {
  393.                 WaitAMoment=1;
  394.                 clear(WaitAMoment);
  395.             }
  396. // Check for plotyy mode
  397.             if (exist (PlotYyParameter)) {
  398.                 if (PlotYyParameter==1) {
  399.                     if (!exist (MultiPlotParameter)) {
  400.                         fprintf (p.[N].prog, "set multiplot\n");
  401.                     }
  402.                     fprintf (p.[N].prog, "set border 7\n");
  403.                 else if (PlotYyParameter==2) {
  404.                     fprintf (p.[N].prog, "set border 8\n");
  405.                 }}
  406.                 PlotYyParameter=PlotYyParameter+1;
  407.             }
  408. // Check for multiplot mode
  409.             if (exist (MultiPlotParameter)) {
  410.                 printf (" Plot number %i/%i done!\n",MultiPlotParameter[3],MultiPlotParameter[1]*MultiPlotParameter[2]);            
  411.                 if (MultiPlotParameter[3]==1) {
  412.                     fprintf (p.[N].prog, "set multiplot\n");
  413.                     fprintf (p.[N].prog, "set size %s,%s\n",1./MultiPlotParameter[2],1./MultiPlotParameter[1]);
  414.                 }
  415.                 N=MultiPlotParameter[4];
  416.                 fprintf (p.[N].prog, "set orgin %s,%s\n",MultiOrginParameter[MultiPlotParameter[3];1],MultiOrginParameter[MultiPlotParameter[3];2]);
  417.                 if (exist (PlotYyParameter)) {
  418.                     if (PlotYyParameter==3) {MultiPlotParameter[3]=MultiPlotParameter[3]+1; }
  419.                 else
  420.                     MultiPlotParameter[3]=MultiPlotParameter[3]+1;
  421.                 }
  422.             }
  423.         }
  424.  
  425.         if (class (datax) != "num") {
  426.             error ("Invalid data class for splot() in x-vector (must be num)");
  427.         else if (class (datay) != "num") {
  428.             error ("Invalid data class for splot() in y-vector (must be num)");
  429.         else if (class (datazz) != "num") {
  430.             error ("Invalid data class for splot() in x-matrix (must be num)");
  431.          else
  432.              fprintf (p.[N].prog, "set parametric\n");
  433.             fprintf (p.[N].prog, "set view 60, 30, 1, 1\n");
  434.             fprintf (p.[N].prog, "set surface\n");
  435.             fprintf (p.[N].prog, "set nocontour\n");
  436.             pobj_Rm (N);
  437.             splotm (datax, datay, datazz, N, 1, titles);
  438.             pobj_Plot (N);
  439.             pobj_SetRm (N);
  440.         }}}
  441.     else
  442.         error ("Invalid data for splot(), bad x/y-range in z-matrix");
  443.     }
  444.     
  445. // Restore the output/input
  446.     if (flag1 == 1) { datax=datazz; }
  447.     if (flag2 == 1) {
  448.         datax=datazz;
  449.         datay=N;
  450.     else if (flag2 == 2) {
  451.         datax=datazz;
  452.         datay=titles;
  453.     }}
  454.     if (flag3 == 1) {
  455.         datax=datazz;
  456.         datay=titles;
  457.         datazz=N;
  458.     }
  459.     if (flag4 == 1) { titles=N; }
  460.     if (flagx == 1) { datax=datax'; }
  461.     if (flagy == 1) { datay=datay'; }
  462.  
  463. // Check for multiplot and/or plotyy mode
  464. // Check for plotyy mode
  465.     if (exist (PlotYyParameter)) {
  466.         if (PlotYyParameter==3) {
  467.             if (!exist (MultiPlotParameter)) { fprintf (p.[N].prog, "set nomultiplot\n"); }
  468.             fprintf (p.[N].prog, "set border\n");
  469.             clear(PlotYyParameter);
  470.         }
  471.     }
  472. // Check for multiplot mode
  473.     if (exist (MultiPlotParameter)) {
  474.         if (MultiPlotParameter[3]==MultiPlotParameter[1]*MultiPlotParameter[2]+1) {
  475.             fprintf (p.[N].prog, "set nomultiplot\n");
  476.             fprintf (p.[N].prog, "set size %s,%s\n",1,1);
  477.             fprintf (p.[N].prog, "set orgin %s,%s\n",0,0);
  478.             fprintf (p.[N].prog, "set border\n");
  479.             clear(MultiPlotParameter);
  480.             clear(MultiOrginParameter);
  481.         }
  482.     }
  483. };
  484.  
  485.  
  486.  
  487. //
  488. // User interface to splot functionality as 2D-contour
  489. //
  490.  
  491. cont = function ( datax, datay, datazz, titles, N )
  492. {
  493.     local (flag1, flag2, flag3, flag4, flagx, flagy, WaitAMoment)
  494.  
  495. // Reset flags
  496.     flag1=0;
  497.     flag2=0;
  498.     flag3=0;
  499.     flag4=0;
  500.     flagx=0;
  501.     flagy=0;
  502.  
  503. // Check what has been sent here
  504. // One input!
  505.     if (!exist (datay)) {
  506.         datazz=datax;
  507.         datax=1:datazz.nc;
  508.         datax=datax';
  509.         datay=1:datazz.nr;
  510.         datay=datay';
  511.         flag1=flag1+1;
  512. // Two inputs!
  513.     else if (!exist (datazz)) {
  514.         if (exist (datay)) {
  515.             if (class (datay) == "num") {
  516.                 N = datay;
  517.                 flag2=flag2+0;
  518.             else
  519.                 titles = datay;
  520.                 flag2=flag2+1;
  521.             }
  522.         }
  523.         datazz=datax;
  524.         datax=1:datazz.nc;
  525.         datax=datax';
  526.         datay=1:datazz.nr;
  527.         datay=datay';
  528.         flag2=flag2+1;
  529. // Three inputs!
  530.     else if (!exist (titles)) {
  531.         if (class (datay) != "num") {
  532.             titles = datay;
  533.             N = datazz;
  534.             datazz=datax;
  535.             datax=1:datazz.nc;
  536.             datax=datax';
  537.             datay=1:datazz.nr;
  538.             datay=datay';
  539.             flag3=flag3+1;
  540.         }
  541. // Four inputs!
  542.     else if (!exist (N)) {
  543.         if (class (titles) == "num") {
  544.             N=titles;
  545.             flag4=flag4+1;
  546.         }
  547.     }}}}
  548.     
  549. // Transpose x- and y-vectors if nessecary
  550.     if (datax.nc != 1 ) {
  551.         if (datax.nr != 1 ) {
  552.             error ("Invalid data for x-axis (not a vector)");
  553.         else
  554.             datax = datax';
  555.             flagx=flagx+1;
  556.         }
  557.     }
  558.     if (datay.nc != 1 ) {
  559.         if (datay.nr != 1 ) {
  560.             error ("Invalid data for y-axis (not a vector)");
  561.         else
  562.             datay = datay';
  563.             flagy=flagy+1;
  564.         }
  565.     }
  566.     
  567. // Check how many splots to do
  568.     plotantal=(datazz.nc/datazz.nr)/(datax.nr/datay.nr);
  569.     if (plotantal == int(plotantal)) {
  570.  
  571.         if (!exist (N)) { N = 0; }    // Always the default splot-object
  572.  
  573. // Check the existence of p.[N]
  574.         if (max (size (N)) != 1) { error ("splot(): N must be a 1-by-1"); }
  575.         if (exist (p.[N])) {
  576.             pobj_Reset (N);
  577.         else
  578.             pobj_Create (N);
  579.         }
  580.  
  581. // Check for multiplot and/or plotyy mode
  582.         if (exist (MultiPlotParameter) || exist (PlotYyParameter)) {
  583. // Delay (RLaB is a bit to fast)
  584.             for(i in 1:1000) {
  585.                 WaitAMoment=1;
  586.                 clear(WaitAMoment);
  587.             }
  588. // Check for plotyy mode
  589.             if (exist (PlotYyParameter)) {
  590.                 if (PlotYyParameter==1) {
  591.                     if (!exist (MultiPlotParameter)) {
  592.                         fprintf (p.[N].prog, "set multiplot\n");
  593.                     }
  594.                     fprintf (p.[N].prog, "set border 7\n");
  595.                 else if (PlotYyParameter==2) {
  596.                     fprintf (p.[N].prog, "set border 8\n");
  597.                 }}
  598.                 PlotYyParameter=PlotYyParameter+1;
  599.             }
  600. // Check for multiplot mode
  601.                 if (exist (MultiPlotParameter)) {
  602.                 if (MultiPlotParameter[3]==1) {
  603.                     fprintf (p.[N].prog, "set multiplot\n");
  604.                     fprintf (p.[N].prog, "set size %s,%s\n",1/MultiPlotParameter[2],1/MultiPlotParameter[1]);
  605.                 }
  606.                 N=MultiPlotParameter[4];
  607.                 fprintf (p.[N].prog, "set orgin %s,%s\n",MultiOrginParameter[MultiPlotParameter[3];1],MultiOrginParameter[MultiPlotParameter[3];2]);
  608.                 if (exist (PlotYyParameter)) {
  609.                     if (PlotYyParameter==3) {MultiPlotParameter[3]=MultiPlotParameter[3]+1; }
  610.                 else
  611.                     MultiPlotParameter[3]=MultiPlotParameter[3]+1;
  612.                 }
  613.             }
  614.         }
  615.  
  616.         if (class (datax) != "num") {
  617.             error ("Invalid data class for splot() in x-vector (must be num)");
  618.         else if (class (datay) != "num") {
  619.             error ("Invalid data class for splot() in y-vector (must be num)");
  620.         else if (class (datazz) != "num") {
  621.             error ("Invalid data class for splot() in x-matrix (must be num)");
  622.          else
  623.              fprintf (p.[N].prog, "set parametric\n");
  624.             fprintf (p.[N].prog, "set view 0, 0, 1\n");
  625.             fprintf (p.[N].prog, "set nosurface\n");
  626.             fprintf (p.[N].prog, "set contour\n");
  627.             pobj_Rm (N);
  628.             splotm (datax, datay, datazz, N, 1, titles);
  629.             pobj_Plot (N);
  630.             pobj_SetRm (N);
  631.         }}}
  632.     else
  633.         error ("Invalid data for splot(), bad x/y-range in z-matrix");
  634.     }
  635.     
  636. // Restore the output/input
  637.     if (flag1 == 1) { datax=datazz; }
  638.     if (flag2 == 1) {
  639.         datax=datazz;
  640.         datay=N;
  641.     else if (flag2 == 2) {
  642.         datax=datazz;
  643.         datay=titles;
  644.     }}
  645.     if (flag3 == 1) {
  646.         datax=datazz;
  647.         datay=titles;
  648.         datazz=N;
  649.     }
  650.     if (flag4 == 1) { titles=N; }
  651.     if (flagx == 1) { datax=datax'; }
  652.     if (flagy == 1) { datay=datay'; }
  653.  
  654. // Check for multiplot and/or plotyy mode
  655. // Check for plotyy mode
  656.     if (exist (PlotYyParameter)) {
  657.         if (PlotYyParameter==3) {
  658.             if (!exist (MultiPlotParameter)) { fprintf (p.[N].prog, "set nomultiplot\n"); }
  659.             fprintf (p.[N].prog, "set border\n");
  660.             clear(PlotYyParameter);
  661.         }
  662.     }
  663. // Check for multiplot mode
  664.     if (exist (MultiPlotParameter)) {
  665.         if (MultiPlotParameter[3]==MultiPlotParameter[1]*MultiPlotParameter[2]+1) {
  666.             fprintf (p.[N].prog, "set nomultiplot\n");
  667.             fprintf (p.[N].prog, "set size %s,%s\n",1,1);
  668.             fprintf (p.[N].prog, "set orgin %s,%s\n",0,0);
  669.             fprintf (p.[N].prog, "set border\n");
  670.             clear(MultiPlotParameter);
  671.             clear(MultiOrginParameter);
  672.         }
  673.     }
  674. };
  675.  
  676.  
  677.  
  678. //
  679. // Send string to GNUPLOT
  680. //
  681.  
  682. pstring = function ( string, N )
  683. {
  684.     if (!exist (N)) { N = 0; }
  685.     if (!exist (p.[N])) { pobj_Create (N); }
  686.     fprintf (p.[N].prog, "%s\n",string);
  687. };
  688.  
  689.  
  690.  
  691. //
  692. // User interface to save a plot ot splot as postscript hardcopy
  693. //
  694.  
  695. postplot = function ( file, mode, N )
  696. {
  697.     local ( flag1 );
  698.     flag1 = 0;
  699.     if (!exist (N) && exist (mode)) {
  700.         if (class (mode) == "num") {
  701.             N = mode;
  702.             flag1 = 1;
  703.         }
  704.     }
  705.  
  706.     if (!exist (N)) { N = 0; }
  707.     if (!exist (p.[N])) { error ("No existing plot to make hardcopy from"); }
  708.  
  709. // Make hardcopy file
  710.     if (!exist (mode)) { mode="default"; }
  711.     if (class (mode) == "num") { mode="default"; }
  712.     fprintf (p.[N].prog, "set term post %s\n", mode);
  713.     fprintf (p.[N].prog, "set output \"%s\"\n", file);
  714.     fprintf (p.[N].prog, "replot\n");
  715.  
  716. // Reset to original term type, and replot
  717.     fprintf (p.[N].prog, "set term %s\n", p.[N].term);
  718.     fprintf (p.[N].prog, "replot\n");
  719.  
  720.     if (flag1 == 1) {
  721.         mode = N;
  722.     }
  723. };
  724.  
  725.  
  726.  
  727. //
  728. // User interface to save a plot ot splot as LaTeX hardcopy
  729. //
  730.  
  731. latexplot = function ( file, mode, N )
  732. {
  733.     local ( flag1 );
  734.     flag1 = 0;
  735.     if (!exist (N) && exist (mode)) {
  736.         if (class (mode) == "num") {
  737.             N = mode;
  738.             flag1 = 1;
  739.         }
  740.     }
  741.  
  742.     if (!exist (N)) { N = 0; }
  743.     if (!exist (p.[N])) { error ("No existing plot to make hardcopy from"); }
  744.  
  745. // Make hardcopy file
  746.     if (!exist (mode)) { mode="default"; }
  747.     if (class (mode) == "num") { mode="default"; }
  748.     fprintf (p.[N].prog, "set term latex %s\n", mode);
  749.     fprintf (p.[N].prog, "set output \"%s\"\n", file);
  750.     fprintf (p.[N].prog, "replot\n");
  751.  
  752. // Reset to original term type, and replot
  753.     fprintf (p.[N].prog, "set term %s\n", p.[N].term);
  754.     fprintf (p.[N].prog, "replot\n");
  755.  
  756.     if (flag1 == 1) {
  757.         mode = N;
  758.     }
  759. };
  760.  
  761.  
  762. //
  763. // User interface to print a plot ot splot
  764. //
  765.  
  766. printplot = function ( printername, N )
  767. {
  768.     local(fn, flag1);
  769.     
  770.     flag1 = 0;
  771.     if (exist (printername) && !exist(N)) {
  772.         if (class (printername) == "num") {
  773.             N = printername;
  774.             clear(printername);
  775.             flag1 = 1;
  776.         }
  777.     }
  778.     
  779.     if (!exist (N)) { N = 0; }
  780.     if (!exist (p.[N])) { pobj_Create (N); }
  781.     
  782. // Create tmp-file-name
  783.     fn = "/usr/tmp/rlab-tmpf.ps";
  784.  
  785.     postplot (fn, "default", I);
  786.     if (exist (printername)) {
  787.         system ("lp -d" + printername + " " + fn);
  788.     else
  789.         system ("lp " + fn);
  790.     }
  791.     system ("rm -f " + fn);
  792.     
  793.     if (flag1 == 1) {
  794.         printername = N;
  795.     }
  796. };
  797.  
  798.  
  799. //
  800. // User interface to save plots as GNUPLOT-datafiles
  801. //
  802.  
  803. psave = function ( file, data )
  804. {
  805.     local (ans, i, nplot, flag1);
  806.  
  807.     flag1 = 0;
  808. // Check file-name
  809.     if (class (file) != "string") {
  810.     error ("Invalid string for file_name");
  811.     }
  812.  
  813. // Determine how many lines to save
  814.     nplot = max ([1, data.nc - 1]);
  815.  
  816.     if (nplot > data.nr) {
  817.         printf (" Save %i columns, are you sure [y(es)/n(o)/t(ranspose) (def. no)] ? ", data.nc);
  818.         ans = getline ("stdin");
  819.         if (ans.[1] != "y" && ans.[1] != "t") {
  820.             return 0;
  821.         }
  822.         if (ans.[1] == "t") {
  823.             data = data';
  824.             nplot = max ([1, data.nc - 1]);
  825.             flag1 = 1;
  826.         }
  827.     }
  828.  
  829. // Generate two column matrices for saving
  830.     for (i in 1:nplot) {
  831.  
  832. // Write data to file
  833.         if (nplot == 1) {
  834.             pobj_WriteData (real (data), file);
  835.         else
  836.             pobj_WriteData (real (data[;1,i+1]), file);
  837.         }
  838.     }
  839.  
  840. // Check flag1
  841.     if (flag1 == 1) {
  842.         data = data';
  843.     }
  844. };
  845.  
  846.  
  847.  
  848. //
  849. // User interface to save splots as GNUPLOT-datafiles
  850. //
  851.  
  852. pssave = function ( file, datax, datay, datazz)
  853. {
  854.     local (ans, i, nsplot, m, n, flag2, flagx, flagy);
  855.  
  856. // Reset flags
  857.     flag2=0;
  858.     flagx=0;
  859.     flagy=0;
  860.  
  861. // Determine how many plots to draw
  862.     if (!exist (datazz)) { 
  863.         datazz=datax;
  864.         datax=1:datazz.nr;
  865.         datax=datax';
  866.         datay=1:datazz.nc;
  867.         datay=datay';
  868.         flag2=flag2+1;
  869.     }
  870.  
  871.     if (datax.nc != 1 ) {
  872.         if (datax.nr != 1 ) {
  873.             error ("Invalid data for x-axis (not a vector)");
  874.         else
  875.             datax = datax';
  876.             flagx=flagx+1;
  877.         }
  878.     }
  879.     if (datay.nc != 1 ) {
  880.         if (datay.nr != 1 ) {
  881.             error ("Invalid data for y-axis (not a vector)");
  882.         else
  883.             datay = datay';
  884.             flagy=flagy+1;
  885.         }
  886.     }
  887.  
  888. // Create file-name
  889.     if (!exist (file)) { error ("Invalid string for file_name"); }
  890.     if (class (file) != "string") { error ("Invalid string for file_name"); }
  891.  
  892.     nsplot=(datazz.nr/datazz.nc)/(datax.nr/datay.nr);
  893.  
  894. // Generate three column matrices for splot program
  895. // Write data to new data-file
  896.     for (i in 1:nsplot) {
  897.         newdata=[];
  898.         for (m in 1:datax.nr){
  899.             rownumber=0;
  900.             for (n in 1:datay.nr){
  901.                 rownumber=rownumber+1;
  902.                 newdata[rownumber;1]=datax[m];                // x-value
  903.                 newdata[rownumber;2]=datay[n];                // y-value
  904.                 newdata[rownumber;3]=datazz[m;n+(i-1)*datax.nr];    // z-value
  905.             }
  906.             write (file,real (newdata[;1,2,3]));
  907.             write (file,"");
  908.         }
  909.         close (file);
  910.     }
  911.     
  912.     if (flag2 == 1) {
  913.         datax=datazz;
  914.     }
  915.     if (flagx == 1) { datax=datax'; }
  916.     if (flagy == 1) { datay=datay'; }
  917.  
  918. };
  919.  
  920.  
  921.  
  922. //
  923. // Make multiplots
  924. //
  925.  
  926. multiplot = function ( row, col, N )
  927. {
  928.     local (i, j, k)
  929.     
  930.     if (!exist (N)) { N = 0; }
  931.     if (!exist (p.[N])) { pobj_Create (N); }
  932.     MultiPlotParameter=[row,col,1,N];
  933.     MultiOrginParameter=[];
  934.     printf (" Make %i plots!\n",MultiPlotParameter[1]*MultiPlotParameter[2]);    
  935.     k=1;
  936.     for (i in 1:row) {
  937.         for (j in 1:col) {
  938.             MultiOrginParameter[k;1]=(j-1)/col;
  939.             MultiOrginParameter[k;2]=(row-i)/row;
  940.             k=k+1;
  941.         }
  942.     }
  943. };
  944.  
  945.  
  946.  
  947. //
  948. // Make plot with two y-axes
  949. //
  950.  
  951. plotyy = function ( N )
  952. {
  953.     
  954.     if (!exist (N)) { N = 0; }
  955.     if (!exist (p.[N])) { pobj_Create (N); }
  956.     printf (" Make 2 plots!\n The first on left y-axis, the second on the right!\n");
  957.     PlotYyParameter=1;
  958. };
  959.  
  960.  
  961.  
  962. //
  963. // User interface to close a GNUPLOT-window
  964. //
  965.  
  966. pclose = function ( N )
  967. {
  968.     local (i)
  969.     if (!exist (N)) { N = 0; }
  970.     if (N == "all") {
  971.         for (i in 0:99) {
  972.             if (exist (p.[i])) {
  973.                 close (p.[i].rmf);
  974.                 close (p.[i].prog);
  975.                 pobj_Destroy (i);
  976.             }
  977.         }
  978.     else
  979.         if (!exist (p.[N])) { return -1; }
  980.         close (p.[N].rmf);
  981.         close (p.[N].prog);
  982.         pobj_Destroy (N);
  983.     }
  984.     return N;
  985. };
  986.  
  987.  
  988.  
  989. //
  990. // Set the terminal type for the I-th GNUPLOT process
  991. //
  992.  
  993. setterm = function ( TERM, N )
  994. {
  995.     if (!exist (N)) { N = 0; }
  996.     if (!exist (p.[N])) { pobj_Create (N); }
  997.     p.[N].term = TERM;
  998.  
  999. // Now send the "set term" command to GNUPLOT
  1000.     fprintf (p.[N].prog, "set term %s\n", TERM);
  1001. };
  1002.  
  1003.  
  1004.  
  1005. //
  1006. // Print out the plot list
  1007. //
  1008.  
  1009. showplot = function ( N )
  1010. {
  1011.     if (!exist (N)) { N = 0; }
  1012.     if (!exist (p.[N])) {
  1013.         pobj_Create (N);
  1014.     }
  1015.  
  1016.     printf ("\tPlot List %i\n", N);
  1017.     printf ("\t\tTerm:\t\t\t%s\n", p.[N].term);
  1018.     printf ("\t\tOutput:\t\t\t%s\n", p.[N].output);
  1019.     printf ("\t\tTmp Files:\t\t%s\n", p.[N].files);
  1020. //    printf ("\t\tHardCopy:\t\t%s\n", p.[N].hrd);
  1021. //    printf ("\t\tHardCopy Files:\t\t%s\n", p.[N].hrdf);
  1022. };
  1023.  
  1024. showpwin = function ()
  1025. {
  1026.     local ( i )
  1027.     for ( i in 0:99) {
  1028.         if (exist (p.[i])) {
  1029.             printf ("\tPlot window number %i exists!\n", i);
  1030.         }
  1031.     }
  1032. };
  1033.  
  1034. //
  1035. // End multiplot mode
  1036. //
  1037.  
  1038. nomultiplot = function(N)
  1039. {
  1040.     if (!exist (N)) { N = 0; }
  1041.     if (!exist (p.[N])) { pobj_Create (N); }
  1042.     fprintf (p.[N].prog, "set size %s,%s\n",1,1);
  1043.     fprintf (p.[N].prog, "set orgin %s,%s\n",0,0);
  1044.     fprintf (p.[N].prog, "set nomultiplot\n");
  1045.     if (exist (MultiPlotParameter)) {clear(MultiPlotParameter);}
  1046.     if (exist (MultiOrginParameter)) {clear(MultiOrginParameter);}
  1047.     if (exist (PlotYyParameter)) {clear(PlotYyParameter);}
  1048. };
  1049.  
  1050.  
  1051.  
  1052. //
  1053. // End plotyy mode
  1054. //
  1055.  
  1056. noplotyy = function(N)
  1057. {
  1058.     if (!exist (N)) { N = 0; }
  1059.     if (!exist (p.[N])) { pobj_Create (N); }
  1060.  
  1061.     fprintf (p.[N].prog, "set nomultiplot\n");
  1062.     if (exist (PlotYyParameter)) {clear(PlotYyParameter);}
  1063. };
  1064.  
  1065.  
  1066.  
  1067. //
  1068. // Here comes a bunch of functions that really is not nessecary.
  1069. // They could be done with string("replot") etc, but this is more convinient.
  1070. //
  1071.  
  1072. replot = function ( N )
  1073. {
  1074.     if (!exist (N)) { N = 0; }
  1075.     if (!exist (p.[N])) {
  1076.         error ("No existing plot to replot");
  1077.     else
  1078.         fprintf (p.[N].prog, "replot\n");
  1079.     }
  1080. };
  1081.  
  1082. xrange = function ( start, end, N )
  1083. {
  1084.     if (!exist (N)) { N = 0; }
  1085.     if (!exist (p.[N])) { pobj_Create (N); }
  1086.     fprintf (p.[N].prog, "set xrange [%s:%s]\n",start,end);
  1087. };
  1088.  
  1089. yrange = function ( start, end, N )
  1090. {
  1091.     if (!exist (N)) { N = 0; }
  1092.     if (!exist (p.[N])) { pobj_Create (N); }
  1093.     fprintf (p.[N].prog, "set yrange [%s:%s]\n",start,end);
  1094. };
  1095.  
  1096. zrange = function ( start, end, N )
  1097. {
  1098.     if (!exist (N)) { N = 0; }
  1099.     if (!exist (p.[N])) { pobj_Create (N); }
  1100.     fprintf (p.[N].prog, "set zrange [%s:%s]\n",start,end);
  1101. };
  1102.  
  1103. xtics = function ( start, end, incr, N )
  1104. {
  1105.     local ( string, i )
  1106.     if (!exist (start)) { start = 0; }
  1107.     if (!exist (incr)) {
  1108.         if (max(size(start)) > 1) {
  1109.             if (!exist (end)) { end = 0; }
  1110.             if (!exist (p.[end])) { pobj_Create (end); }
  1111.             string = "set xtics (" + num2str(start[1])[1];
  1112.             for (i in 2:max(size(start))) {
  1113.                 string = string + "," + num2str(start[i])[1];
  1114.             }
  1115.             string = string + ")\n";
  1116.             fprintf (p.[end].prog, string);
  1117.         else
  1118.             if (!exist (p.[start])) { pobj_Create (start); }
  1119.             fprintf (p.[start].prog, "set xtics\n");
  1120.         }
  1121.     else
  1122.         if (!exist (N)) { N = 0; }
  1123.         if (!exist (p.[N])) { pobj_Create (N); }
  1124.         fprintf (p.[N].prog, "set xtics %s,%s,%s\n",start,incr,end);
  1125.     }
  1126. };
  1127.  
  1128. noxtics = function ( N )
  1129. {
  1130.     if (!exist (N)) { N = 0; }
  1131.     if (!exist (p.[N])) { pobj_Create (N); }
  1132.     fprintf (p.[N].prog, "set noxtics\n");
  1133. };
  1134.  
  1135. ytics = function ( start, end, incr, N )
  1136. {
  1137.     local ( string, i )
  1138.     if (!exist (start)) { start = 0; }
  1139.     if (!exist (incr)) {
  1140.         if (max(size(start)) > 1) {
  1141.             if (!exist (end)) { end = 0; }
  1142.             if (!exist (p.[end])) { pobj_Create (end); }
  1143.             string = "set ytics (" + num2str(start[1])[1];
  1144.             for (i in 2:max(size(start))) {
  1145.                 string = string + "," + num2str(start[i])[1];
  1146.             }
  1147.             string = string + ")\n";
  1148.             fprintf (p.[end].prog, string);
  1149.         else
  1150.             if (!exist (p.[start])) { pobj_Create (start); }
  1151.             fprintf (p.[start].prog, "set ytics\n");
  1152.         }
  1153.     else
  1154.         if (!exist (N)) { N = 0; }
  1155.         if (!exist (p.[N])) { pobj_Create (N); }
  1156.         fprintf (p.[N].prog, "set ytics %s,%s,%s\n",start,incr,end);
  1157.     }
  1158. };
  1159.  
  1160. noytics = function ( N )
  1161. {
  1162.     if (!exist (N)) { N = 0; }
  1163.     if (!exist (p.[N])) { pobj_Create (N); }
  1164.     fprintf (p.[N].prog, "set noytics\n");
  1165. };
  1166.  
  1167. ztics = function ( start, end, incr, N )
  1168. {
  1169.     local ( string, i )
  1170.     if (!exist (start)) { start = 0; }
  1171.     if (!exist (incr)) {
  1172.         if (max(size(start)) > 1) {
  1173.             if (!exist (end)) { end = 0; }
  1174.             if (!exist (p.[end])) { pobj_Create (end); }
  1175.             string = "set ztics (" + num2str(start[1])[1];
  1176.             for (i in 2:max(size(start))) {
  1177.                 string = string + "," + num2str(start[i])[1];
  1178.             }
  1179.             string = string + ")\n";
  1180.             fprintf (p.[end].prog, string);
  1181.         else
  1182.             if (!exist (p.[start])) { pobj_Create (start); }
  1183.             fprintf (p.[start].prog, "set ztics\n");
  1184.         }
  1185.     else
  1186.         if (!exist (N)) { N = 0; }
  1187.         if (!exist (p.[N])) { pobj_Create (N); }
  1188.         fprintf (p.[N].prog, "set ztics %s,%s,%s\n",start,incr,end);
  1189.     }
  1190. };
  1191.  
  1192. noztics = function ( N )
  1193. {
  1194.     if (!exist (N)) { N = 0; }
  1195.     if (!exist (p.[N])) { pobj_Create (N); }
  1196.     fprintf (p.[N].prog, "set noztics\n");
  1197. };
  1198.  
  1199. loglog = function ( N )
  1200. {
  1201.     if (!exist (N)) { N = 0; }
  1202.     if (!exist (p.[N])) { pobj_Create (N); }
  1203.     fprintf (p.[N].prog, "set nologscale\n");
  1204.     fprintf (p.[N].prog, "set logscale xy\n");
  1205. };
  1206.  
  1207. semilogx = function ( N )
  1208. {
  1209.     if (!exist (N)) { N = 0; }
  1210.     if (!exist (p.[N])) { pobj_Create (N); }
  1211.     fprintf (p.[N].prog, "set logscale x\n");
  1212. };
  1213.  
  1214. semilogy = function ( N )
  1215. {
  1216.     if (!exist (N)) { N = 0; }
  1217.     if (!exist (p.[N])) { pobj_Create (N); }
  1218.     fprintf (p.[N].prog, "set logscale y\n");
  1219. };
  1220.  
  1221. nolog = function ( N )
  1222. {
  1223.     if (!exist (N)) { N = 0; }
  1224.     if (!exist (p.[N])) { pobj_Create (N); }
  1225.     fprintf (p.[N].prog, "set nologscale\n");
  1226. };
  1227.  
  1228. autoscale = function ( axis, N )
  1229. {
  1230.     local( flag )
  1231.     flag = 0;
  1232.     if (!exist (axis) && !exist (N)) {
  1233.         N = 0;
  1234.         axis = "xy";
  1235.     }
  1236.     if (exist (axis) && !exist (N)) {
  1237.         if (class (axis) == "num") {
  1238.             N = axis;
  1239.             axis = "xy";
  1240.             flag = 1;
  1241.         }
  1242.     }
  1243.     if (!exist (p.[N])) { pobj_Create (N); }
  1244.     if (class(axis) != "string") {
  1245.         error ("Must be of class string");
  1246.     }
  1247.     fprintf (p.[N].prog, "set autoscale %s\n",axis);
  1248.     if (flag == 1) { axis = N; }
  1249. };
  1250.  
  1251. xlabel = function ( string, N )
  1252. {
  1253.     if (!exist (N)) { N = 0; }
  1254.     if (!exist (p.[N])) { pobj_Create (N); }
  1255.     fprintf (p.[N].prog, "set xlabel \"%s\"\n",string);
  1256. };
  1257.  
  1258. ylabel = function ( string, N )
  1259. {
  1260.     if (!exist (N)) { N = 0; }
  1261.     if (!exist (p.[N])) { pobj_Create (N); }
  1262.     fprintf (p.[N].prog, "set ylabel \"%s\"\n",string);
  1263. };
  1264.  
  1265. zlabel = function ( string, N )
  1266. {
  1267.     if (!exist (N)) { N = 0; }
  1268.     if (!exist (p.[N])) { pobj_Create (N); }
  1269.     fprintf (p.[N].prog, "set zlabel \"%s\"\n",string);
  1270. };
  1271.  
  1272. title = function ( string, N )
  1273. {
  1274.     if (!exist (N)) { N = 0; }
  1275.     if (!exist (p.[N])) { pobj_Create (N); }
  1276.     fprintf (p.[N].prog, "set title \"%s\"\n",string);
  1277. };
  1278.  
  1279. notitle = function ( N )
  1280. {
  1281.     if (!exist (N)) { N = 0; }
  1282.     if (!exist (p.[N])) { pobj_Create (N); }
  1283.     fprintf (p.[N].prog, "set title\n");
  1284. };
  1285.  
  1286. linestyle = function ( string, N )
  1287. {
  1288.     if (!exist (N)) { N = 0; }
  1289.     if (!exist (p.[N])) { pobj_Create (N); }
  1290.     fprintf (p.[N].prog, "set data style %s\n",string);
  1291. };
  1292.  
  1293. pformat = function ( string1, string2, N )
  1294. {
  1295.     local( flag1);
  1296.     flag1 = 0;
  1297.     if (!exist(string2)) {
  1298.         string2 = "xy";
  1299.     }
  1300.     if (exist (string2) && !exist (N)) {
  1301.         if (class (string2) == "num") {
  1302.             N = string2;
  1303.             string2 = "xy";
  1304.             flag1 = 1;
  1305.         }
  1306.     }
  1307.     if (!exist (N)) { N = 0; }
  1308.     if (!exist (p.[N])) { pobj_Create (N); }
  1309.     fprintf (p.[N].prog, "set format %s \"%s\"\n",string2,string1);
  1310.     if (flag1 == 1) {
  1311.         string1 = string2;
  1312.         string2 = N;
  1313.     }
  1314. };
  1315.  
  1316.  
  1317. key = function ( x, y, z, N )
  1318. {
  1319.     local ( flag1 )
  1320.     flag1 = 0;
  1321.     if (exist(x) && !exist(y)) {
  1322.         N = x;
  1323.         flag1 = 1;
  1324.     }
  1325.     if (!exist (z)) { z = 0; }
  1326.     if (!exist (N)) { N = 0; }
  1327.     if (!exist (p.[N])) { pobj_Create (N); }
  1328.     if (exist(y)) {
  1329.         fprintf (p.[N].prog, "set key %s,%s,%s\n",x,y,z);
  1330.     else
  1331.         fprintf (p.[N].prog, "set key\n");
  1332.     }
  1333.     if (flag1 == 1) {
  1334.         x = N;
  1335.     }
  1336. };
  1337.  
  1338. nokey = function ( N )
  1339. {
  1340.     if (!exist (N)) { N = 0; }
  1341.     if (!exist (p.[N])) { pobj_Create (N); }
  1342.     fprintf (p.[N].prog, "set nokey\n");
  1343. };
  1344.  
  1345. grid = function ( N )
  1346. {
  1347.     if (!exist (N)) { N = 0; }
  1348.     if (!exist (p.[N])) { pobj_Create (N); }
  1349.     fprintf (p.[N].prog, "set grid\n");
  1350. };
  1351.  
  1352. nogrid = function ( N )
  1353. {
  1354.     if (!exist (N)) { N = 0; }
  1355.     if (!exist (p.[N])) { pobj_Create (N); }
  1356.     fprintf (p.[N].prog, "set nogrid\n");
  1357. };
  1358.  
  1359. axis = function ( N )
  1360. {
  1361.     if (!exist (N)) { N = 0; }
  1362.     if (!exist (p.[N])) { pobj_Create (N); }
  1363.     fprintf (p.[N].prog, "set xzeroaxis\n");
  1364.     fprintf (p.[N].prog, "set yzeroaxis\n");
  1365. };
  1366.  
  1367. noaxis = function ( N )
  1368. {
  1369.     if (!exist (N)) { N = 0; }
  1370.     if (!exist (p.[N])) { pobj_Create (N); }
  1371.     fprintf (p.[N].prog, "set noxzeroaxis\n");
  1372.     fprintf (p.[N].prog, "set noyzeroaxis\n");
  1373. };
  1374.  
  1375. label = function ( label, x, y, z, labelnr, N )
  1376. {
  1377.     if (!exist (N)) { N = 0; }
  1378.     if (!exist (p.[N])) { pobj_Create (N); }
  1379.     if (class(label) != "string") {
  1380.         error ("The label must be a string!")
  1381.     }
  1382.     if (exist (labelnr)) {
  1383.         fprintf (p.[N].prog, "set label %s \"%s\" at %s,%s\n", label, labelnr , x, y, z);
  1384.     else
  1385.         fprintf (p.[N].prog, "set label \"%s\" at %s,%s\n", label, x, y, z);
  1386.     }
  1387. };
  1388.  
  1389. nolabel = function ( labelnr, N )
  1390. {
  1391.     if (!exist (N)) { N = 0; }
  1392.     if (!exist (p.[N])) { pobj_Create (N); }
  1393.     if (exist (labelnr)) {
  1394.         fprintf (p.[N].prog, "set nolabel %s\n", labelnr);
  1395.     else
  1396.         fprintf (p.[N].prog, "set nolabel\n");
  1397.     }
  1398. };
  1399.  
  1400. time = function ( N )
  1401. {
  1402.     if (!exist (N)) { N = 0; }
  1403.     if (!exist (p.[N])) { pobj_Create (N); }
  1404.     fprintf (p.[N].prog, "set time\n");
  1405. };
  1406.  
  1407. notime = function ( N )
  1408. {
  1409.     if (!exist (N)) { N = 0; }
  1410.     if (!exist (p.[N])) { pobj_Create (N); }
  1411.     fprintf (p.[N].prog, "set notime\n");
  1412. };
  1413.  
  1414.     
  1415. //====================
  1416. // Static Functions    =
  1417. //====================
  1418.  
  1419. plotl = function ( data, N, titles )
  1420. {
  1421.     local (I, i);
  1422.     I = 1;
  1423.     for (i in members (data)) {
  1424.         if (class (data.[i]) == "num") {
  1425. // Check for plotting titles
  1426.     spobj_PlotKeyTitle (titles, I, title );
  1427.  
  1428.             plotm (data.[i], N, I, title);
  1429.             I++;
  1430.         }
  1431.     }
  1432. };
  1433.  
  1434.  
  1435.  
  1436. //
  1437. // Set-Up to plot a matrix. Columns 2...N against 1st column.
  1438. //
  1439.  
  1440. plotm = function ( data, N, I, titles )
  1441. {
  1442.     local (ans, fn, i, nplot, title);
  1443.     
  1444.     flag1 = 0;
  1445. // Determine how many lines to draw
  1446.     nplot = max ([1, data.nc - 1]);
  1447.  
  1448.     if (nplot > data.nr) {
  1449.         printf (" Plot %i columns, are you sure [y(es)/n(o)/t(ranspose) (def. no)] ? ", data.nc);
  1450.         ans = getline ("stdin");
  1451.         if (ans.[1] != "y" && ans.[1] != "t") {
  1452.             return 0;
  1453.         }
  1454.         if (ans.[1] == "t") {
  1455.             data = data';
  1456.             nplot = max ([1, data.nc - 1]);
  1457.             flag1 = 1;
  1458.         }
  1459.     }
  1460.  
  1461. // Generate two column matrices for plot program
  1462.     for (i in 1:nplot) {
  1463. // Create tmp-file-name
  1464.     fn = pobj_TmpFileName (N, i, I);
  1465.  
  1466. // Check for plotting titles
  1467.     spobj_PlotKeyTitle (titles, i, title );
  1468.  
  1469. // Write data to tmp-file
  1470. // Add to plot command
  1471.         if (nplot == 1) {
  1472.             pobj_WriteData (real (data), fn);
  1473.             pobj_PlotCmd (N, fn, title);
  1474.         else
  1475.             pobj_WriteData (real (data[;1,i+1]), fn);
  1476.             pobj_PlotCmd (N, fn, title);
  1477.         }
  1478.     }
  1479.     
  1480. // Check flag1
  1481.     if (flag1 == 1) {
  1482.         data = data';
  1483.     }
  1484.  
  1485. };
  1486.  
  1487.  
  1488.  
  1489. //
  1490. // Set-Up to splot a matrix. 
  1491. //
  1492.  
  1493. splotm = function ( datax, datay, datazz, N, I, titles )
  1494. {
  1495.     local (ans, fn, i, nsplot, m, n, title);
  1496.  
  1497. // Determine how many plots to draw
  1498.     nsplot=(datazz.nc/datazz.nr)/(datax.nr/datay.nr);
  1499.  
  1500. // Generate three column matrices for splot program
  1501. // Write data to new data-file
  1502. // Create tmp-file-name
  1503.     for (i in 1:nsplot) {
  1504.         fn = pobj_TmpFileName (N, i, I);
  1505.  
  1506. // Check for plotting titles
  1507.     spobj_PlotKeyTitle (titles, I, title );
  1508.         newdata=[];
  1509.         for (m in 1:datax.nr){
  1510.             rownumber=0;
  1511.             for (n in 1:datay.nr){
  1512.                 rownumber=rownumber+1;
  1513.                 newdata[rownumber;1]=datax[m];                // x-value
  1514.                 newdata[rownumber;2]=datay[n];                // y-value
  1515.                 newdata[rownumber;3]=datazz[m;n+(i-1)*datax.nr];    // z-value
  1516.             }
  1517.             write (fn,real (newdata[;1,2,3]));
  1518.             write (fn,"");
  1519.         }
  1520.         close (fn);
  1521. // Add to splot command
  1522.     spobj_PlotCmd (N, fn, title);
  1523.     }
  1524. };
  1525.  
  1526.  
  1527.  
  1528. //
  1529. // Set-Up to splot a matrix. 
  1530. //
  1531.  
  1532.  
  1533.  
  1534.  
  1535. //
  1536. // Form a plain string to send to GNUPLOT as 
  1537. // command
  1538. //
  1539.  
  1540. plots = function ( data, N )
  1541. {
  1542. // Send the string to GNUPLOT
  1543.  
  1544.     p.[N].pcmd = data + "\n";
  1545. };
  1546.  
  1547.  
  1548.  
  1549. //
  1550. // Create a plot-object.
  1551. //
  1552.  
  1553. pobj_Create = function ( N )
  1554. {
  1555.     local (plist);
  1556.  
  1557.     plist.files = "";        // The tmp files to plot
  1558.     plist.pcmd = "";        // Where the plot command will go
  1559.  
  1560. // Init string for plotting program
  1561.     plist.init = "set nogrid\nset data style lines\nset nokey\nset noxzeroaxis\nset noyzeroaxis\n";
  1562.  
  1563. // The program that draws the plot(s)
  1564.     sprintf (plist.prog, "|gnuplot #%i", N);
  1565.  
  1566. // To remove tmp-files
  1567.  
  1568.     sprintf (plist.rmf, "|rm -f `cat` #%i", N);
  1569.  
  1570. // Keep track of terminal type, and output
  1571.     plist.term = "X11";
  1572.     plist.output = "stdout";
  1573.  
  1574. // Copy the local list into the static plot-object collection
  1575.     p.[N] = plist;
  1576.     fprintf (p.[N].prog, "%s", p.[N].init);
  1577.     fprintf (p.[N].prog, "set term %s\n", p.[N].term);
  1578. };
  1579.  
  1580.  
  1581.  
  1582. //
  1583. // Reset a plot object to plot new data
  1584. //
  1585.  
  1586. pobj_Reset = function ( N )
  1587. {
  1588.  
  1589. // Close any existing tmp-files that belong
  1590. // to plot-object N. Reset the file-name list.
  1591.     p.[N].files = "";
  1592.     p.[N].pcmd = "";
  1593. };
  1594.  
  1595.  
  1596.  
  1597. //
  1598. // Destroy a plot-object
  1599. //
  1600.  
  1601. pobj_Destroy = function ( N )
  1602. {
  1603.     if (exist (p.[N])) {
  1604.         clear (p.[N]);    
  1605.     }
  1606. };
  1607.  
  1608.  
  1609.  
  1610. //
  1611. // Create a tmp-file name
  1612. //
  1613.  
  1614. pobj_TmpFileName = function ( N, i, j )
  1615. {
  1616.     local (tmp);
  1617.     sprintf (tmp, "/usr/tmp/rlab-tmpf-%i-%i-%i", N, i, j);
  1618.     p.[N].files = p.[N].files + " " + tmp;
  1619.     return tmp;
  1620. };
  1621.  
  1622.  
  1623.  
  1624. //
  1625. // Add data-file (tmp-file) to plot object list of stuff
  1626. // to be plotted.
  1627. //
  1628.  
  1629. pobj_WriteData = function ( m, file )
  1630. {
  1631.     write (file, m);
  1632.     close (file);
  1633. };
  1634.  
  1635.  
  1636.  
  1637. //
  1638. // Create the command(s) to plot all the data in the plot object.
  1639. //
  1640.  
  1641. pobj_PlotCmd = function ( N, fn, title )
  1642. {
  1643.     local (tmp);
  1644.  
  1645.     if(p.[N].pcmd == "") {
  1646.         sprintf(tmp, "plot '%s' title '%s'", fn, title);
  1647.     else
  1648.         sprintf(tmp,        ", '%s' title '%s'", fn, title);
  1649.     }
  1650.  
  1651.     p.[N].pcmd = p.[N].pcmd + tmp;
  1652. };
  1653.  
  1654.  
  1655.  
  1656. //
  1657. // Create the command(s) to splot all the data in the splot object.
  1658. //
  1659.  
  1660. spobj_PlotCmd = function ( N, fn, title )
  1661. {
  1662.     local (tmp);
  1663.  
  1664.     if(p.[N].pcmd == "") {
  1665.         sprintf(tmp, "splot '%s' title '%s'", fn, title);
  1666.     else
  1667.         sprintf(tmp,        ", '%s' title '%s'", fn, title);
  1668.     }
  1669.  
  1670.     p.[N].pcmd = p.[N].pcmd + tmp;
  1671. };
  1672.  
  1673.  
  1674.  
  1675. //
  1676. // Check for plotting titles
  1677. //
  1678.  
  1679. spobj_PlotKeyTitle = function (titles, i, title )
  1680. {
  1681.     if (!exist (titles)) {
  1682.         sprintf (title, "%.4g", i+1);
  1683.         title = "C-" + title;
  1684.     else
  1685.         if (i > max(size(titles))) {
  1686.             sprintf (title, "%.4g", i+1);
  1687.             title = "C-" + title;
  1688.         else
  1689.             title = titles[i];
  1690.         }
  1691.     }
  1692. };
  1693.  
  1694.  
  1695.  
  1696. //
  1697. // Force the plot program to create the plot
  1698. //
  1699.  
  1700. pobj_Plot = function ( N )
  1701. {
  1702. //    fprintf ("stdout", "%s\n", p.[N].pcmd);
  1703.     fprintf (p.[N].prog, "%s\n", p.[N].pcmd);
  1704. };
  1705.  
  1706.  
  1707.  
  1708. //
  1709. // Setup so the tmp-files can be removed
  1710. //
  1711.  
  1712. pobj_SetRm = function ( N )
  1713. {
  1714.     fprintf(p.[N].rmf, "%s", p.[N].files);
  1715. };
  1716.  
  1717.  
  1718.  
  1719. //
  1720. // Remove the tmp-files
  1721. //
  1722.  
  1723. pobj_Rm = function ( N )
  1724. {
  1725.     if (length (p.[N].files) != 0) {
  1726.         close (p.[N].rmf);
  1727.     }
  1728. };
  1729.  
  1730.  
  1731.  
  1732. //
  1733. // Provide access to the plot-list
  1734. //
  1735.  
  1736. _pobj_list = function ( N )
  1737. {
  1738.     if (!exist (N)) { N = 0; }
  1739.     return p.[N];
  1740. };
  1741.  
  1742.